import os,sys
import RPi.GPIO as GPIO
import subprocess
import threading
from time import sleep
from multiprocessing import Process
touchOut = 24
touchPin = 18

sleep(20)

def run_livox():
	os.system('sudo ifconfig eth0 up & sudo ifconfig eth0 192.168.3.21 netmask 255.255.255.0 & sudo /home/ubuntu/sh/lidar_lvx_sample')

def run_lpms():
	os.system('sudo /home/ubuntu/sh/LpmsIG1_SimpleExample /dev/gd_USB 115200')

def run_pps():
	os.system('sudo python pps.py')

def run_rtk():
#	os.system('sudo stty -F /dev/gd_USB ispeed 115200 ospeed 115200 & sudo stty -F /dev/rtk_USB ispeed 115200 ospeed 115200 & sudo /home/ubuntu/sh/main')
	os.system('sudo stty -F /dev/rtk_USB ispeed 115200 ospeed 115200 & sudo /home/ubuntu/sh/main')

def run_main():
	os.system('sudo stty -F /dev/rtk_USB ispeed 115200 ospeed 115200 & sudo ifconfig eth0 up & sudo ifconfig eth0 192.168.3.21 netmask 255.255.255.0 & sudo /home/ubuntu/sh/main')

def close_process(name):
	print 'j---------------------------------'
	child = subprocess.Popen(["pgrep","-f",name],stdout=subprocess.PIPE,shell=False)
	response = child.communicate()[0]
	list_r = response.split('\n')[:-1]

	for i in list_r:
		os.system("kill -15 "+i)	

def get_process_id(name):
	child = subprocess.Popen(["pgrep","-f",name],stdout=subprocess.PIPE,shell=False)
	response = child.communicate()[0]
	list_r = response.split('\n')[:-1]
	for i in list_r:
		os.system("kill -9 "+i)

def check_process(name):
    child = subprocess.Popen(["pgrep","-f",name],stdout=subprocess.PIPE,shell=False)
    response = child.communicate()[0]
    if not response:
        #print 'no '+name + 'started'
        return 0
    else:
        #print  name + 'is running'
        return 1          

if __name__ == "__main__":
        
    
    GPIO.setmode(GPIO.BCM)
    '''
    GPIO.setup(touchOut,GPIO.OUT)
    while 1:
        GPIO.output(touchOut,0)
        sleep(1)
        GPIO.output(touchOut,1)
        sleep(1)
    
    '''
    GPIO.setup(touchPin,GPIO.IN)
    
    temp = 1
    while 1:
        
        if GPIO.input(touchPin) == 1:
        	print 'high'
		sleep(0.2)
		if GPIO.input(touchPin) == 1 and temp == 1:
			temp = 0
			threading.Thread(target=run_main).start()
			#sleep(5)
	  		#threading.Thread(target=run_lpms).start()
			#threading.Thread(target=run_pps).start()
			#threading.Thread(target=run_rtk).start()
			print 'jing---------------------------------------'
			#os.system('sudo ifconfig eth0 up & sudo ifconfig eth0 192.168.3.21 netmask 255.255.255.0 & sudo /home/ubuntu/sh/lidar_lvx_sample')			
			#sleep(5)				
			#os.system('sudo /home/ubuntu/sh/LpmsIG1_SimpleExample /dev/ttyUSB0 115200')
			#sleep(1)
			#os.system('sudo /home/ubuntu/sh/pps.sh')			
			#sleep(1)
			#os.system('sudo /home/ubuntu/sh/rtk.sh')
			print 'chu----------------------------------------'

        if GPIO.input(touchPin) == 0:
	        print 'low'
		sleep(0.2)
		if GPIO.input(touchPin) == 0 and temp == 0:
			temp = 1
			#close_process("pps.py")
			close_process("main")
			#close_process("lidar_lvx_sample")
			#close_process("LpmsIG1_SimpleExample")
	        	#os.system('killall -9 main')
			#os.system('killall -9 lidar_lvx_sample')
			#os.system('killall -9 LpmsIG1_SimpleExample')
			#pid = get_process_id("sudo python pps.py")
			#os.system('kill -9 '+pid)
        ret = check_process("main")    
        if ret:
	        #print 'main is runnning'
	        temp = 0
        else:
	        print 'main is not started'
	        temp = 1
    GPIO.cleanup()
